package dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.Horder;
import model.Hotel;
import model.View;
import util.EntityManagerUtil;

public class HotelDao {
	//得到所有的hotel
	public static List<Hotel> getHotels() {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Query query = em.createQuery("SELECT c FROM Hotel c ");
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		List<Hotel> hotels = query.getResultList();
		em.close();
		return hotels;
	}
	public static Hotel getHotel(Hotel hotel) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Hotel hotelFind = new Hotel();
		hotelFind = em.find(Hotel.class, hotel.getEmail());
		if (hotelFind != null) {
			em.close();
			return hotelFind;
		} else
			return null;
	}
	
	public static Hotel getHotelNoCache(String hemail) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Hotel c where c.email=:email";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Hotel hotelFind;
		try {
			hotelFind = (Hotel) query.setParameter("email", hemail).getSingleResult();
			em.close();
			return hotelFind;
		} catch (Exception e) {
			System.out.println("查找失败");
			return null;
		}
	}
	
	public static boolean getHotelNoCacheForBool(String hemail) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Hotel c where c.email=:email";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Hotel hotelFind;
		try {
			hotelFind = (Hotel) query.setParameter("email", hemail).getSingleResult();
			em.close();
			return true;
		} catch (Exception e) {
			System.out.println("查找失败");
			return false;
		}
	}
	public static Hotel getHotelNoCache(Hotel hotel) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Hotel c where c.email=:email";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Hotel hotelFind = (Hotel) query.setParameter("email", hotel.getEmail()).getSingleResult();
		em.close();
		if (hotelFind != null)
			return hotelFind;
		else
			return null;
	}

	// ��ӾƵ���Ϣ success
	public static boolean addHotel(Hotel hotel) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Hotel hotelFind = getHotelNoCache(hotel);
		} catch (Exception e) {
			em.getTransaction().begin();
			em.persist(hotel);
			em.getTransaction().commit();
			em.close();
			return true;
		}
		return false;
	}

	public static boolean delHotel(Hotel hotel) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Hotel hotelFind = getHotelNoCache(hotel);
			if (hotelFind != null) {
				Hotel delHotel = em.find(Hotel.class, hotel.getEmail());
				em.getTransaction().begin();
				List<Horder> horders = new ArrayList<Horder>();
				horders = delHotel.getHorders();
				for(Horder horder: horders){
					HorderDao.delHorder(horder);
				}
				em.remove(delHotel);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}


		public static boolean updateHotel(Hotel hotel) throws Exception {
			EntityManager em = EntityManagerUtil.getEntityManager();
			try {
				Hotel hotelFind = getHotelNoCache(hotel);
				if (hotelFind != null) {
					Hotel upHotel = em.find(Hotel.class, hotel.getEmail());
					em.getTransaction().begin();
					upHotel.setInfo(hotel.getInfo());
					upHotel.setName(hotel.getName());
					upHotel.setLocation(hotel.getLocation());
					upHotel.setPrice(hotel.getPrice());
					List<Horder> horders = new ArrayList<Horder>();
					horders = upHotel.getHorders();
					for(Horder horder: horders){
						HorderDao.updateHorder(horder);
					}
					
					upHotel.setSum(hotel.getSum());
					em.persist(upHotel);
					em.getTransaction().commit();
					em.close();
					return true;
				} else {
					return false;
				}
			} catch (Exception e) {
				return false;
			}

		}
	public static void main(String argsp[]) throws Exception {
		Hotel hotel = new Hotel();
		hotel.setEmail("t1@qq.com");
		System.out.println(HotelDao.getHotel(hotel).getLocation());
		System.out.println(HotelDao.getHotelNoCache(hotel).getLocation());
		System.out.println(HotelDao.getHotel(hotel).getHroutes().get(0).getRoute().getRname());
	//	Hotel hotel1 = new Hotel();
	//	hotel1.setEmail("b1b1");
	//	hotel1.setInfo("11112221111");
	//	updateHotel(hotel1);
		//addHotel(hotel1);
		//delHotel(hotel1);
	}

}
